   /**
	 * Interface graphique de la gestion client
	* @author I.T. Works.
	* @date 22/12/2011
	* @version 1.0
	*/

	package org.itworks.biodev.gui.panels;

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.sql.SQLException;
    import java.util.*;

    import javax.swing.*;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;
    import javax.swing.table.DefaultTableModel;

    import org.itworks.biodev.gui.*;
    import org.itworks.biodev.model.Customer;
    import org.itworks.biodev.model.Meeting;
    import org.itworks.biodev.model.MeetingTable;
    import org.itworks.helpers.MVCNotification;

    import org.itworks.biodev.model.*;
	import org.itworks.helpers.*;
	import org.itworks.helpers.gui.ButtonColumn;


    public class CustomersPanelView extends JPanel implements Observer{
    
        /* Observer constants */
        public static final int FILL_CUSTOMERS_TABLE = 1;
    
        private static CustomersPanelView customersPanelView = null;
    
        private CustomersPanelControler controler;
    
        private MainWindowView parent; 
    
        private javax.swing.JButton createNewCustomerButton;
        private javax.swing.JComboBox criteriaSelectionCombobox;
        private javax.swing.JButton modifyExistingCustomerButton;
        private javax.swing.JTable customersListTable;
        private javax.swing.JLabel customersTitleLabel;
        private javax.swing.JButton deleteExistingCustomerButton;
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JButton searchByCriteriaButton;
        private javax.swing.JTextField searchByCriteriaTextField;
        
        
        private CustomersPanelView(MainWindowView parent, CustomersPanelControler controler) 
        {
            this.parent = parent;
            this.controler = controler;
            this.controler.getModel().addObserver(this);
            initComponents();
            initHandlers();
        
            controler.CustomersPanelOnLoad();
        }

        /*
         * Gestion des actions sur les boutons
         */
        private void initHandlers() {
            // TODO Auto-generated method stub
        	//Bouton nouveau client
            this.createNewCustomerButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent actionEvent) {
                    // TODO Auto-generated method stub
                    controler.createNewCustomerButtonActionPerformed(actionEvent);
                }
            });
            //Bouton modifier un client
            this.modifyExistingCustomerButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent actionEvent) {
                    Vector<Integer> selectedIndexes = new Vector<Integer>();
                    /* Pour chaque ligne */
                    for(int i = 0; i < customersListTable.getRowCount(); i++) {
                        /* Si la case est coch�e */
                        if(customersListTable.getValueAt(i, 9).toString().equals("true")) {
                            selectedIndexes.add(Integer.parseInt(customersListTable.getValueAt(i, 0).toString()));
                        }
                    }
                    controler.modifyExistingCustomerButtonActionPerformed(actionEvent, selectedIndexes);
                }

            });

            //bouton supprimer un client
            this.deleteExistingCustomerButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent actionEvent) {
                    Vector<Integer> selectedIndexes = new Vector<Integer>();
                    /* Pour chaque ligne */
                    for(int i = 0; i < customersListTable.getRowCount(); i++) {
                        /* Si la case est coch�e */
                        if(customersListTable.getValueAt(i, 9).toString().equals("true")) {
                        	//On le supprime
                            selectedIndexes.add(Integer.parseInt(customersListTable.getValueAt(i, 0).toString()));
                        }
                    }
                    controler.deleteExistingCustomerButtonActionPerformed(actionEvent, selectedIndexes);
                }
            });


            this.customersListTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
                @Override
                public void valueChanged(ListSelectionEvent listSelectionEvent) {
                    // TODO Auto-generated method stub7
                
                    try {
                        Customer selecteCustomer;
                        int selectedRow = customersListTable.getSelectedRow();
                        if(selectedRow > -1) {
                            int selectedIndex = Integer.parseInt(customersListTable.getValueAt(selectedRow, 0).toString());
                            if(selectedIndex > -1) {
                                selecteCustomer = CustomerTable.getInstance().find(selectedIndex);
                            }
                        }
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            });

        }

        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {

            searchByCriteriaTextField = new javax.swing.JTextField();
        searchByCriteriaButton = new javax.swing.JButton();
        criteriaSelectionCombobox = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        customersListTable = new javax.swing.JTable();
        createNewCustomerButton = new javax.swing.JButton();
        deleteExistingCustomerButton = new javax.swing.JButton();
        customersTitleLabel = new javax.swing.JLabel();
        modifyExistingCustomerButton = new javax.swing.JButton();

        searchByCriteriaTextField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchByCriteriaTextFieldActionPerformed(evt);
            }
        });

        searchByCriteriaButton.setText("chercher");

        criteriaSelectionCombobox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nom", "Prenom", "Ville" }));
        criteriaSelectionCombobox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                criteriaSelectionComboboxActionPerformed(evt);
            }
        });

        customersListTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {         },
            new String [] {
                    "ID", "Prenom", "Nom", "Adresse", "Ville", "Code postal", "Email", "Telephone", "Modifier", "Supprimer"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.String.class, java.lang.Boolean.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
            boolean[] canEdit = new boolean [] {
                    false, false, false, false, false, false, false, false, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        customersListTable.getTableHeader().setReorderingAllowed(false);
        jScrollPane1.setViewportView(customersListTable);
        customersListTable.getColumnModel().getColumn(0).setResizable(false);
        customersListTable.getColumnModel().getColumn(0).setPreferredWidth(10);

        createNewCustomerButton.setText("Ajouter un client");

        deleteExistingCustomerButton.setText("Supprimer un client");

        customersTitleLabel.setFont(new java.awt.Font("Trebuchet MS", 1, 18));
        customersTitleLabel.setText("Clients");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(55, 55, 55)
                        .addComponent(searchByCriteriaTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(criteriaSelectionCombobox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(searchByCriteriaButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(createNewCustomerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(deleteExistingCustomerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 859, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(customersTitleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(39, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(customersTitleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(searchByCriteriaTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(criteriaSelectionCombobox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(searchByCriteriaButton))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(53, 53, 53)
                        .addComponent(createNewCustomerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(deleteExistingCustomerButton, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 458, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(172, Short.MAX_VALUE))
        );
        }// </editor-fold>

        private void criteriaSelectionComboboxActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // TODO add your handling code here:
        }                                          

        private void searchByCriteriaTextFieldActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
        }                                           

        private void createNewCustomerButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                        
        // TODO add your handling code here:
        }                                                       

        private void deleteExistingCustomerButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        }
        private void modifyExistingCustomerButtonActionPerformed(java.awt.event.ActionEvent evt) {
            // TODO Auto-generated method stub
        
        }
    
        @Override
        public void update(Observable srcObject, Object object)  
        {
            // TODO Auto-generated method stub
                MVCNotification notification = (MVCNotification)object;
                int reason = notification.getReason();
            
                switch(reason) {
                case FILL_CUSTOMERS_TABLE:
                    // todo.
                    try {
                        fillCustomersTable( (Vector<Customer>)notification.getObject() );
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    break;
                }
        }
        private void fillCustomersTable(Vector<Customer> customers) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException 
        {
            DefaultTableModel model = new javax.swing.table.DefaultTableModel(
                new Object [][] {
                },
                new String [] {
                        "ID", "Prenom", "Nom", "Adresse", "Ville", "Code postal", "Email", "Telephone","Modifier", "Supprimer"
                }
            ) {
                Class[] types = new Class [] {
                    java.lang.Integer.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class,  java.lang.String.class, java.lang.Boolean.class
                };

                public Class getColumnClass(int columnIndex) {
                    return types [columnIndex];
                }
                boolean[] canEdit = new boolean [] {
                    false, false, false, false, false, false, false, false, true, true
                };

                public boolean isCellEditable(int rowIndex, int columnIndex) {
                    return canEdit [columnIndex];
                }
            };
        
        
            customersListTable.setModel(model);
        
            for(Customer c : customers) {
                Vector<Object> rowData = new Vector<Object>();
                rowData.add(c.getId()+"");
                rowData.add(c.getFirstName()+"");
                rowData.add(c.getLastName()+"");
                rowData.add(c.getAddress()+"");
                rowData.add(c.getCity()+"");
                rowData.add(c.getPostalCode()+"");
                rowData.add(c.getMail()+"");
                rowData.add(c.getPhoneNumber()+"");
                rowData.add("Modifier");
    			/**
    			 * Ajout d'un bouton dans la ligne à 9 eme place (l'indice commence a 0)
    			 */
    			new ButtonColumn(customersListTable, new AbstractAction() {
    				private static final long serialVersionUID = 5911305847353251180L;

    				@Override
    				public void actionPerformed(ActionEvent actionEvent) {
    					int selectedIndex = customersListTable.getSelectedRow();
    					controler.buttonColumnCustomersActionPerformed(actionEvent, selectedIndex);
    					
    				}
    				
    			}, 8);
    			rowData.add(new Boolean(false));
    			model.addRow(rowData);
            }
        
        
        }
        public static CustomersPanelView getInstance(MainWindowView parent) {
            if(customersPanelView == null) {
                customersPanelView = new CustomersPanelView(parent, new CustomersPanelControler(new CustomersPanelModel()));
            }
        
            return customersPanelView;
        }
    }
